library(survey)
library(foreign)
library(ggplot2)
library(cowplot)
library(dplyr)
library(egg)
library(ggpubr)

## see PCM_cleaning.R for data cleaning
load("~/Dropbox/Public_Conf_Mil/Data_and_code/clean_data.RData")

## Create weighted survey design object
milconf_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

insts_pa <- svyby(~ Q15A_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
insts_go <- svyby(~ Q15B_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
insts_re <- svyby(~ Q15C_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
insts_et <- svyby(~ Q15D_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
insts_tc <- svyby(~ Q15E_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
insts_sv <- svyby(~ Q15F_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
insts_ot <- svyby(~ Q15G_d, ~ DOV_INSTITUTION, milconf_design, svymean, na.rm = TRUE)
mil_pa <- svymean(~ Q12A_d, milconf_design, na.rm = TRUE)
mil_go <- svymean(~ Q12B_d, milconf_design, na.rm = TRUE)
mil_re <- svymean(~ Q12C_d, milconf_design, na.rm = TRUE)
mil_et <- svymean(~ Q12D_d, milconf_design, na.rm = TRUE)
mil_tc <- svymean(~ Q12E_d, milconf_design, na.rm = TRUE)
mil_sv <- svymean(~ Q12F_d, milconf_design, na.rm = TRUE)
mil_ot <- svymean(~ Q12H_d, milconf_design, na.rm = TRUE)

#### FIGURE 6.5 - INSTITUTION ATTRIBUTES, BAR CHART ####

atts_df <- data.frame(pa = c(mil_pa[1]*100, insts_pa[,2]*100),
                      go = c(mil_go[1]*100, insts_go[,2]*100),
                      re = c(mil_re[1]*100, insts_re[,2]*100),
                      et = c(mil_et[1]*100, insts_et[,2]*100),
                      tc = c(mil_tc[1]*100, insts_tc[,2]*100),
                      sv = c(mil_sv[1]*100, insts_sv[,2]*100),
                      insts = c(1:5))

pa_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = pa), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  ggtitle("Too Partisan") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
pa_plot

go_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = go), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  ggtitle("Good at What It Does") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
go_plot

re_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = re), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  ggtitle("Representative of the US") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
re_plot

et_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = et), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  ggtitle("Ethical") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
et_plot

tc_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = tc), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  ggtitle("Makes Truthful Claims") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
tc_plot

sv_plot <- ggplot(atts_df) +
  geom_col(aes(x = insts, y = sv), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  ggtitle("Shares My Values") +
  xlab("") + ylab("") + ylim(0,100) + theme_bw()
sv_plot

ggarrange(pa_plot + theme(axis.text.x = element_text(size = 6),
                          plot.title = element_text(size = 12)), 
          go_plot + theme(axis.text.y = element_blank(),
                          axis.ticks.y = element_blank(),
                          axis.title.y = element_blank(),
                          axis.text.x = element_text(size = 6),
                          plot.title = element_text(size = 12)), 
          re_plot + theme(axis.text.y = element_blank(),
                          axis.ticks.y = element_blank(),
                          axis.title.y = element_blank(),
                          axis.text.x = element_text(size = 6),
                          plot.title = element_text(size = 12)), 
          et_plot + theme(axis.text.x = element_text(size = 6),
                          plot.title = element_text(size = 12)), 
          tc_plot + theme(axis.text.y = element_blank(),
                          axis.ticks.y = element_blank(),
                          axis.title.y = element_blank(),
                          axis.text.x = element_text(size = 6),
                          plot.title = element_text(size = 12)), 
          sv_plot + theme(axis.text.y = element_blank(),
                          axis.ticks.y = element_blank(),
                          axis.title.y = element_blank(),
                          axis.text.x = element_text(size = 6),
                          plot.title = element_text(size = 12)),
          nrow = 2, ncol = 3, rel)

#### FIGURE 6.6 - OTHERS CONFIDENCE ####

ot_df <- data.frame(ot = c(mil_ot[1]*100, insts_ot[,2]*100),
                    insts = c(1:5))

ot_plot <- ggplot(ot_df) +
  geom_col(aes(x = insts, y = ot), fill = "gray60", width = 0.5) +
  scale_x_continuous(breaks = c(1,2,3,4,5),
                     labels = c("Military", "SCOTUS", "Congress",
                                "Media", "FP Elite")) +
  xlab("") + ylab("Respondents Who Agree (%)") + ylim(0,80) + theme_bw()
ot_plot

#### FIGURE 6.7 - PCA ####

what <- svyprcomp(~ Q12A_d+Q12B_d+Q12C_d+Q12D_d+Q12E_d+Q12F_d+Q15A_d+Q15B_d+Q15C_d+
            Q15D_d+Q15E_d+Q15F_d, milconf_design, scale = TRUE)
summary(what)
what
